package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
   @Autowired
   private CategoryBrandService categoryBrandService;

    @Override
    public List<CategoryDTO> queryCategoryByPid(Long pid) {
        //select * from tb_category where parent_id = pid;
        List<Category> list = query().eq("parent_id", pid).list();
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(list);
        return categoryDTOS;

    }

    @Override
    public List<CategoryDTO> queryByBrandId(Long id) {
        //根据品牌id查询中间表，获取中间表的对象集合
        List<CategoryBrand> categoryBrands = this.categoryBrandService
                .query()
                .eq("brand_id", id)
                .list();

        //从中间表的对象集合中获取分类id的集合
        List<Long> categoryIds = categoryBrands
                .stream()
                .map(CategoryBrand::getCategoryId)
                .collect(Collectors.toList());



        return queryCategoryById (categoryIds);
    }

    @Override
    public CategoryDTO queryGoodsByGid(Long id) {


        return new CategoryDTO(this.getById(id));

    }

    @Override
    public List<CategoryDTO> queryCategoryById(List<Long> ids) {
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(this.listByIds(ids));

        return categoryDTOS;
    }
}
